/*     */ package com.zimbra.cs.imap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import com.zimbra.cs.mailbox.MetadataList;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import java.io.Serializable;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ class ImapCredentials
/*     */   implements Serializable
/*     */ {
/*     */   private static final long serialVersionUID = -3323076274740054770L;
/*     */   private static final String SN_IMAP = "imap";
/*     */   private static final String FN_SUBSCRIPTIONS = "subs";
/*     */   private final String mAccountId;
/*     */   private final String mUsername;
/*     */   private final boolean mIsLocal;
/*     */   private final EnabledHack mEnabledHack;
/*     */   private Set<ImapPath> mHiddenFolders;
/*     */   
/*     */   static enum EnabledHack
/*     */   {
/*  45 */     NONE,  WM5("/wm"),  THUNDERBIRD("/tb"),  NO_IDLE("/ni");
/*     */     
/*     */     private EnabledHack() {}
/*     */     
/*  49 */     private EnabledHack(String ext) { this.extension = ext; }
/*     */     
/*  51 */     public String toString() { return this.extension; }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */     private String extension;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   ImapCredentials(Account acct, EnabledHack hack)
/*     */     throws ServiceException
/*     */   {
/*  64 */     this.mAccountId = acct.getId();
/*  65 */     this.mUsername = acct.getName();
/*  66 */     this.mIsLocal = Provisioning.onLocalServer(acct);
/*  67 */     this.mEnabledHack = (hack == null ? EnabledHack.NONE : hack);
/*     */   }
/*     */   
/*     */   String getUsername() {
/*  71 */     return this.mUsername;
/*     */   }
/*     */   
/*     */   boolean isLocal() {
/*  75 */     return this.mIsLocal;
/*     */   }
/*     */   
/*     */   boolean isHackEnabled(EnabledHack hack) {
/*  79 */     return this.mEnabledHack == hack;
/*     */   }
/*     */   
/*     */   EnabledHack[] getEnabledHacks() {
/*  83 */     if ((this.mEnabledHack == null) || (this.mEnabledHack == EnabledHack.NONE))
/*  84 */       return null;
/*  85 */     return new EnabledHack[] { this.mEnabledHack };
/*     */   }
/*     */   
/*     */   String getAccountId() {
/*  89 */     return this.mAccountId;
/*     */   }
/*     */   
/*     */   Account getAccount() throws ServiceException {
/*  93 */     return Provisioning.getInstance().get(Key.AccountBy.id, this.mAccountId);
/*     */   }
/*     */   
/*     */   OperationContext getContext() throws ServiceException {
/*  97 */     return new OperationContext(this.mAccountId);
/*     */   }
/*     */   
/*     */   Mailbox getMailbox() throws ServiceException {
/* 101 */     if (!this.mIsLocal)
/* 102 */       throw ServiceException.WRONG_HOST(getAccount().getMailHost(), null);
/* 103 */     return MailboxManager.getInstance().getMailboxByAccountId(this.mAccountId);
/*     */   }
/*     */   
/*     */   private Set<String> parseConfig(Metadata config) throws ServiceException
/*     */   {
/* 108 */     if ((config == null) || (!config.containsKey("subs")))
/* 109 */       return null;
/* 110 */     MetadataList slist = config.getList("subs", true);
/* 111 */     if ((slist == null) || (slist.isEmpty()))
/* 112 */       return null;
/* 113 */     Set<String> subscriptions = new HashSet(slist.size());
/* 114 */     for (int i = 0; i < slist.size(); i++)
/* 115 */       subscriptions.add(slist.get(i));
/* 116 */     return subscriptions;
/*     */   }
/*     */   
/*     */   private void saveConfig(Set<String> subscriptions) throws ServiceException {
/* 120 */     MetadataList slist = new MetadataList();
/* 121 */     if ((subscriptions != null) && (!subscriptions.isEmpty())) {
/* 122 */       for (String sub : subscriptions)
/* 123 */         slist.add(sub);
/*     */     }
/* 125 */     getMailbox().setConfig(getContext(), "imap", new Metadata().put("subs", slist));
/*     */   }
/*     */   
/*     */   Set<String> listSubscriptions() throws ServiceException {
/* 129 */     return parseConfig(getMailbox().getConfig(getContext(), "imap"));
/*     */   }
/*     */   
/*     */   void subscribe(ImapPath path) throws ServiceException {
/* 133 */     Set<String> subscriptions = listSubscriptions();
/* 134 */     String upcase; if ((subscriptions != null) && (!subscriptions.isEmpty())) {
/* 135 */       upcase = path.asImapPath().toUpperCase();
/* 136 */       for (String sub : subscriptions) {
/* 137 */         if (upcase.equals(sub.toUpperCase()))
/* 138 */           return;
/*     */       }
/*     */     }
/* 141 */     if (subscriptions == null)
/* 142 */       subscriptions = new HashSet();
/* 143 */     subscriptions.add(path.asImapPath());
/* 144 */     saveConfig(subscriptions);
/*     */   }
/*     */   
/*     */   void unsubscribe(ImapPath path) throws ServiceException {
/* 148 */     Set<String> subscriptions = listSubscriptions();
/* 149 */     if ((subscriptions == null) || (subscriptions.isEmpty()))
/* 150 */       return;
/* 151 */     String upcase = path.asImapPath().toUpperCase();
/* 152 */     boolean found = false;
/* 153 */     for (Iterator<String> it = subscriptions.iterator(); it.hasNext();) {
/* 154 */       if (upcase.equals(((String)it.next()).toUpperCase())) {
/* 155 */         it.remove();found = true;
/*     */       }
/*     */     }
/* 158 */     if (!found)
/* 159 */       return;
/* 160 */     saveConfig(subscriptions);
/*     */   }
/*     */   
/*     */   void hideFolder(ImapPath path) {
/* 164 */     if (this.mHiddenFolders == null)
/* 165 */       this.mHiddenFolders = new HashSet();
/* 166 */     this.mHiddenFolders.add(path);
/*     */   }
/*     */   
/*     */   boolean isFolderHidden(ImapPath path) {
/* 170 */     return this.mHiddenFolders == null ? false : this.mHiddenFolders.contains(path);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/imap/ImapCredentials.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */